﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;

namespace ConcreteMappersAndObjects.Mappers
{
    public class RoomsDataMapper : AbstractDataMapper<int, Room>
    {
        protected override int GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "select ID, Description, Seats from Room";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ID = @ID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        protected override Room DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            return new Room(GetKeyFrom(sdr), sdr.GetString(1), sdr.GetInt32(2));
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, Room t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "RoomInsert";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, Room t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 20).Value = t.Description;
            cmd.Parameters.Add("@Seats", SqlDbType.Int, 0).Value = t.Seats;
        }


        protected override string GetRemoveCommand()
        {
            return "delete from Room where ID = @ID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, Room t)
        {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = t.ID;
        }

        protected override void FinishUpdateObject(Room t)
        {
            
        }

        protected override void FinishNewObject(Room t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Room t)
        {
            
        }
    }
}
